package network.t_02_nio.zero_copy;

import lombok.Cleanup;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.Socket;

public class BIOFileClient {
    public static void main(String[] args) throws Exception {
        @Cleanup Socket socket = new Socket("localhost", 6666);
        @Cleanup DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataOutputStream.writeUTF(".zip"); // 先发送上传文件的后缀
        @Cleanup InputStream inputStream = new FileInputStream("MyNetty/src/test/resources/protoc-3.11.4-win64.zip");
        byte[] buffer = new byte[4096];
        long total = 0;
        long readCount;
        long startTime = System.currentTimeMillis();
        while ((readCount = inputStream.read(buffer)) >= 0) {
            total += readCount;
            dataOutputStream.write(buffer);
        }
        dataOutputStream.flush();
        socket.shutdownOutput(); // 告诉服务端文件上传完毕
        System.out.println("发送总字节数： " + total + ", 耗时： " + (System.currentTimeMillis() - startTime));
    }
}
